package com.promise.jokerdream.navigation

import android.webkit.WebView
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import com.promise.jokerdream.ui.account.AccountManagementPage
import com.promise.jokerdream.ui.log.ExecutionLogPage
import com.promise.jokerdream.ui.settings.SettingsPage
import com.promise.jokerdream.ui.test.XmlParseTestPage
import com.promise.jokerdream.ui.bag.BagPage
import com.promise.jokerdream.ui.tower.FightTowerPage
import com.promise.jokerdream.ui.wulin.WuLinSearchPage
import com.promise.jokerdream.ui.escort.PenguinEscortPage
import com.promise.jokerdream.ui.territory.TerritoryPage
import com.promise.jokerdream.ui.snapup.SnapUpPage
import com.promise.jokerdream.ui.timer.TimerPage
import com.promise.jokerdream.ui.timer.TimerTaskConfigPage
import com.promise.jokerdream.ui.quickexchange.QuickExchangePage
import com.promise.jokerdream.ui.customtask.CustomTaskPage
import com.promise.jokerdream.ui.customtask.TaskConfigPage
import com.promise.jokerdream.ui.robterritory.RobTerritoryPage
import com.promise.jokerdream.ui.fightfriend.FightFriendPage
import com.promise.jokerdream.ui.penguininfo.PenguinInfoPage
import com.promise.jokerdream.ui.webview.WebViewScreen
import com.promise.jokerdream.viewmodel.MainViewModel

/**
 * 应用导航图
 */
@Composable
fun AppNavGraph(
    navController: NavHostController,
    startDestination: String = AppRoute.Main.route,
    onWebViewCreated: (WebView) -> Unit = {},
    viewModel: MainViewModel
) {
    
    NavHost(
        navController = navController,
        startDestination = startDestination
    ) {
        composable(AppRoute.Main.route) {
            WebViewScreen(
                navController = navController,
                viewModel = viewModel,
                onWebViewCreated = onWebViewCreated
            )
        }
        
        composable(AppRoute.AccountManagement.route) {
            AccountManagementPage(
                context = LocalContext.current,
                navController = navController,
                onAddAccount = {
                    // 处理添加账号逻辑
                    viewModel.clearAllCookies()
                    viewModel.loadUrl(com.promise.jokerdream.network.HttpApiConstants.PhoneUrls.home)
                },
                onAccountSelected = { account ->
                    // 处理账号选择逻辑
                    viewModel.setCurrentAccount(account)
                    viewModel.applyAccountCookie(account)
                }
            )
        }
        
        composable(AppRoute.ExecutionLog.route) {
            ExecutionLogPage(navController = navController)
        }
        
        composable(AppRoute.Settings.route) {
            SettingsPage(navController = navController)
        }
        
        composable(AppRoute.XmlParseTest.route) {
            XmlParseTestPage(
                onBack = {
                    navController.popBackStack()
                }
            )
        }
        
        composable(AppRoute.AutoPlay.route) {
            SettingsPage(navController = navController)
        }
        
        composable(AppRoute.Bag.route) {
            BagPage(
                onBackClick = {
                    navController.popBackStack()
                },
                onNavigateToExecutionLog = {
                    navController.popBackStack()
                    navController.navigate(AppRoute.ExecutionLog.route)
                }
            )
        }
        
        composable(AppRoute.TowerFight.route) {
            FightTowerPage(
                onBackClick = {
                    navController.popBackStack()
                }
            )
        }
        
        composable(AppRoute.WuLinSearch.route) {
            WuLinSearchPage(
                onBackClick = {
                    navController.popBackStack()
                },
                onNavigateToExecutionLog = {
                    navController.popBackStack()
                    navController.navigate(AppRoute.ExecutionLog.route)
                }
            )
        }
        
        composable(AppRoute.Escort.route) {
            PenguinEscortPage(
                onBackClick = {
                    navController.popBackStack()
                },
                onNavigateToExecutionLog = {
                    navController.popBackStack()
                    navController.navigate(AppRoute.ExecutionLog.route)
                }
            )
        }
        
        composable(AppRoute.Territory.route) {
            TerritoryPage(
                onBackClick = {
                    navController.popBackStack()
                },
                onNavigateToExecutionLog = {
                    navController.popBackStack()
                    navController.navigate(AppRoute.ExecutionLog.route)
                }
            )
        }
        
        composable(AppRoute.SnapUp.route) {
            SnapUpPage(
                onBackClick = {
                    navController.popBackStack()
                },
                onNavigateToExecutionLog = {
                    navController.popBackStack()
                    navController.navigate(AppRoute.ExecutionLog.route)
                }
            )
        }
        
        composable(AppRoute.Timer.route) {
            val currentAccount by viewModel.currentAccount.collectAsState()
            
            currentAccount?.let { account ->
                TimerPage(
                    userId = account.userId,
                    navController = navController,
                    onBackClick = {
                        navController.popBackStack()
                    },
                    onNavigateToExecutionLog = {
                        navController.popBackStack()
                        navController.navigate(AppRoute.ExecutionLog.route)
                    }
                )
            }
        }
        
        composable(AppRoute.TimerTaskConfig.route) { backStackEntry ->
            val taskId = backStackEntry.arguments?.getString("taskId") ?: ""
            val currentAccount by viewModel.currentAccount.collectAsState()
            
            currentAccount?.let { account ->
                TimerTaskConfigPage(
                    userId = account.userId,
                    taskId = taskId,
                    onBackClick = {
                        navController.popBackStack()
                    },
                    onNavigateToExecutionLog = {
                        navController.navigate(AppRoute.ExecutionLog.route)
                    }
                )
            }
        }
        
        composable(AppRoute.QuickExchange.route) {
            QuickExchangePage(
                onBackClick = {
                    navController.popBackStack()
                },
                onNavigateToExecutionLog = {
                    navController.popBackStack()
                    navController.navigate(AppRoute.ExecutionLog.route)
                }
            )
        }
        
        composable(AppRoute.CustomTask.route) {
            val currentAccount by viewModel.currentAccount.collectAsState()
            currentAccount?.let { account ->
                CustomTaskPage(
                    userId = account.userId,
                    onBackClick = {
                        navController.popBackStack()
                    },
                    onNavigateToExecutionLog = {
                        navController.popBackStack()
                        navController.navigate(AppRoute.ExecutionLog.route)
                    },
                    navController = navController,
                )
            }
        }
        
        composable(AppRoute.RobTerritory.route) {
            RobTerritoryPage(
                onBackClick = {
                    navController.popBackStack()
                },
                onNavigateToExecutionLog = {
                    navController.popBackStack()
                    navController.navigate(AppRoute.ExecutionLog.route)
                }
            )
        }
        
        composable(AppRoute.FightFriend.route) {
            FightFriendPage(
                onBackClick = {
                    navController.popBackStack()
                },
                onNavigateToExecutionLog = {
                    navController.popBackStack()
                    navController.navigate(AppRoute.ExecutionLog.route)
                }
            )
        }
        
        composable(AppRoute.TaskConfig.route) { backStackEntry ->
            val taskGroupId = backStackEntry.arguments?.getString("taskGroupId") ?: ""
            val currentAccount by viewModel.currentAccount.collectAsState()
            
            currentAccount?.let { account ->
                TaskConfigPage(
                    userId = account.userId,
                    taskGroupId = taskGroupId,
                    onBackClick = {
                        navController.popBackStack()
                    },
                    onNavigateToExecutionLog = {
                        navController.popBackStack()
                        navController.navigate(AppRoute.ExecutionLog.route)
                    }
                )
            }
        }
        
        composable(AppRoute.PenguinInfo.route) { backStackEntry ->
            val puin = backStackEntry.arguments?.getString("puin") ?: ""
            PenguinInfoPage(
                puin = puin,
                onBack = {
                    navController.popBackStack()
                }
            )
        }
    }
}
